/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.preferences.pages;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipsejcde.core.Activator;
import org.eclipsejcde.core.preferences.JCPreferenceConstants;

/**
 * @author Amgad S Youssef
 *
 */
public class ConverterPreferences extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
	BooleanFieldEditor cap;

	BooleanFieldEditor exp;

	BooleanFieldEditor jca;

	BooleanFieldEditor mask;

	public ConverterPreferences() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Set Converter preferences");
	}

	@Override
	public void init(IWorkbench workbench) {
	}

	@Override
	protected void createFieldEditors() {
		cap = new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_out_cap, "Instructs the Converter to output the CAP file", getFieldEditorParent());
		addField(cap);
		exp = new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_out_exp, "Instructs the Converter to output the EXP file", getFieldEditorParent());
		addField(exp);
		jca = new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_out_jca, "Instructs the Converter to output the JCA file", getFieldEditorParent());
		addField(jca);
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_debug, "Generates the optional debug component of a CAP file", getFieldEditorParent()));
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_exportmap, "Uses the token mapping from the pre-defined export file of the package being converted",
				getFieldEditorParent()));
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_i, "Instructs the Converter to support the 32-bit integer type", getFieldEditorParent()));
		mask = new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_mask, "Indicates this package is for a mask, so restrictions on native methods are relaxed", getFieldEditorParent());
		addField(mask);
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_nobanner, "Suppresses all banner messages", getFieldEditorParent()));
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_noverify, "Suppresses the verification of input and output files", getFieldEditorParent()));
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_nowarn, "Instructs the Converter not to report warning messages", getFieldEditorParent()));
		addField(new BooleanFieldEditor(JCPreferenceConstants.CONVERTER_CL_OPTIONS_verbose, "Enables verbose output", getFieldEditorParent()));
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (validatePage()) {
			super.propertyChange(event);
		}
	}

	@Override
	public boolean performOk() {
		if (!validatePage()) {
			return false;
		}
		return super.performOk();
	}

	private boolean validatePage() {
		setErrorMessage(null);
		if (!cap.getBooleanValue() && !exp.getBooleanValue() && !jca.getBooleanValue()) {
			setErrorMessage("You must select at least one output file");
			return false;
		}
		if (cap.getBooleanValue() && mask.getBooleanValue()) {
			setErrorMessage("You can not generate CAP and mask at the same time");
			return false;
		}
		return true;
	}

}
